<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="execute">点击我执行命令</button>
    <button id="undo">点击我执行命令</button>
    <script>
    //   var Tv = {
    //     open: function () {
    //       console.log("打开电视机");
    //     },
    //     close: function () {
    //       console.log("关上电视机");
    //     },
    //   };

    //   var OpenTvCommand = function (receiver) {
    //     this.receiver = receiver;
    //   };

    //   OpenTvCommand.prototype.execute = function () {
    //     this.receiver.open(); // 执行命令，打开电视机
    //   };

    //   OpenTvCommand.prototype.undo = function () {
    //     this.receiver.close(); // 撤销命令，关闭电视机
    //   };

    //   var setCommand = function (command) {
    //     document.getElementById("execute").onclick = function () {
    //       command.execute(); // 输出：打开电视机
    //     };
    //     document.getElementById("undo").onclick = function () {
    //       command.undo(); // 输出：关闭电视机
    //     };
    //   };

    //   setCommand(new OpenTvCommand(Tv));

      var Tv = {
            open: function(){
              console.log( '’打开电视机’' );
            },
            close: function(){
                console.log( '’关上电视机’' );
            }
        };

        var createCommand = function( receiver ){

            var execute = function(){
                return receiver.open();    // 执行命令，打开电视机
            }

            var undo = function(){
                return receiver.close();    // 执行命令，关闭电视机
            }

            return {
                execute: execute,
                undo: undo
            }

        };

        var setCommand = function( command ){
            document.getElementById( 'execute' ).onclick = function(){
                command.execute();     // 输出：打开电视机
            }
            document.getElementById( 'undo' ).onclick = function(){
                command.undo();    // 输出：关闭电视机
            }
        };

        setCommand( createCommand( Tv ) );
    </script>
  </body>
</html>
